package com.intellagric.service.traceability.Impl.trace_produce;

import com.github.pagehelper.PageHelper;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.dao.traceability.ProduceDao;
import com.intellagric.mapper.TraceProduceMapper;
import com.intellagric.module.traceability.ProduceVo;
import com.intellagric.pojo.TraceProduce;
import com.intellagric.pojo.TraceProduceExample;
import com.intellagric.service.traceability.trace_produce.ProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/31
 * @DESC： 溯源农产品信息管理服务实现
 */
@Service
public class ProduceServiceImpl implements ProduceService {

    @Autowired
    private TraceProduceMapper produceMapper;

    @Autowired
    private ProduceDao produceDao;

    /**
     * 添加产品信息
     * @param produce
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addProduct(TraceProduce produce) {
        if (produceMapper.insertSelective(produce) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除农产品信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteProduct(String id) {
        if (produceMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改农产品信息
     * @param produce
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editProduct(TraceProduce produce) {
        if (produceMapper.updateByPrimaryKeySelective(produce) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 通过id查询农产品信息
     * @param id
     * @return TraceProduce
     */
    @Override
    public TraceProduce getProductById(String id) {
        return produceMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询农产品信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProductList() {
        TraceProduceExample produceExample = new TraceProduceExample();
        produceExample.createCriteria().andIdIsNotNull();
        List<TraceProduce> produceList = produceMapper.selectByExampleWithBLOBs(produceExample);
        return LayuiDataGridResult.success().add(produceList,produceList.size());
    }
    @Override
    public LayuiDataGridResult getProductList(String createBy) {
        TraceProduceExample produceExample = new TraceProduceExample();
        produceExample.createCriteria().andCreateByEqualTo(createBy);
        List<TraceProduce> produceList = produceMapper.selectByExampleWithBLOBs(produceExample);
        return LayuiDataGridResult.success().add(produceList,produceList.size());
    }

    /**
     * 分页查询农产品信息列表
     * @param page
     * @param rows
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProductPage(int page, int rows) {
        PageHelper.startPage(page,rows);
        TraceProduceExample produceExample = new TraceProduceExample();
        produceExample.createCriteria().andIdIsNotNull();
        List<TraceProduce> produceList = produceMapper.selectByExampleWithBLOBs(produceExample);
        return LayuiDataGridResult.success().add(produceList,produceList.size());
    }
    @Override
    public LayuiDataGridResult getProductPage(int page, int rows,String createBy) {
        PageHelper.startPage(page,rows);
        TraceProduceExample produceExample = new TraceProduceExample();
        produceExample.createCriteria().andCreateByEqualTo(createBy);
        List<TraceProduce> produceList = produceMapper.selectByExampleWithBLOBs(produceExample);
        return LayuiDataGridResult.success().add(produceList,produceList.size());
    }

    /**
     * 多条件查询农产品信息列表
     * @param produceVo
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getProductByVo(ProduceVo produceVo) {
        List<TraceProduce> produceList = produceDao.selectProduceByVo(produceVo);
        return LayuiDataGridResult.success().add(produceList,produceList.size());
    }

}
